-- title: Nerf Battle
-- author: Landon
-- desc: Game
-- site: https://rachel.likespizza.com/web-stuff/show-and-tell/
-- license: ???
-- version: 0.1
-- script: lua
x2=200; y2=100 -- Player coordinates
damage=0 -- Damage to player
bx=-100; by=-100 -- Bullet coordinates
ex=100; ey=50 -- Enemy coordinates
mapx=0; mapy=0 -- Map location
-- HELPER FUNCTIONS ------------------------------
function IsOnThisMap( obj )
return obj.mapX == mapx and obj.mapY == mapy
end
function GetDistance( x1, y1, x2, y2 )
return math.sqrt( ( x2 - x1 )^2 + ( y2 - y1 )^2 )
end
-- DEBUG -----------------------------------------
function Debug( title, thing, x, y, level )
print( title .. ": " .. type(thing), x + (level*8), y)
if thing == nil then
return
end
for key, value in pairs( thing ) do
y = y + 8
if type( value ) == "table" then
Debug( key, value, x + (level*8), y, level+1 )
else
print( key .. "=" .. value, x + (level*8), y )
end
end
end
function Debug2()
print( "MAP: " .. mapx .. "," .. mapy, 2, 128 )
print( "PLAYER: " .. x .. "," .. y, 50, 128 )
end
-- WARP HANDLER ----------------------------------
warps = {}
warptimer = 0
function CreateWarp( tile1X, tile1Y, tile2X, tile2Y )
newWarp = {}
newWarp.tileX = tile1X
newWarp.tileY = tile1Y
newWarp.mapX = math.floor( newWarp.tileX / 30 ) * 30
newWarp.mapY = math.floor( newWarp.tileY / 16 ) * 16
newWarp.x = (newWarp.tileX - newWarp.mapX)*8
newWarp.y = (newWarp.tileY - newWarp.mapY)*8
newWarp.tileDestX = tile2X
newWarp.tileDestY = tile2Y
newWarp.mapDestX = math.floor( newWarp.tileDestX / 30 ) * 30
newWarp.mapDestY = math.floor( newWarp.tileDestY / 16 ) * 16
newWarp.xDest = (newWarp.tileDestX - newWarp.mapDestX)*8
newWarp.yDest = (newWarp.tileDestY - newWarp.mapDestY)*8
table.insert( warps, newWarp )
end
function HandleWarps()
if warptimer == 0 then
for id, wp in pairs( warps ) do
if IsOnThisMap( wp ) then
if GetDistance( x, y, wp.x, wp.y ) <= 8 then
mapx = wp.mapDestX; mapy = wp.mapDestY
x = wp.xDest; y = wp.yDest
warptimer = 100
end
end -- IsOnMap
end -- for
elseif warptimer > 0 then
warptimer = warptimer - 1
end -- warptimer
end -- function
-- ENEMY HANDLER ---------------------------------
enemies = {}
function CreateEnemy( mapX, mapY, tileX, tileY, health, imgTopLeft, imgTopRight, imgBottomLeft, imgBottomRight )
newEnemy = {}
newEnemy.mapX = mapX
newEnemy.mapY = mapY
newEnemy.tileX = tileX
newEnemy.tileY = tileY
newEnemy.x = (tileX - mapX) * 8
newEnemy.y = (tileY - mapY) * 8
newEnemy.health = health
newEnemy.imgTopLeft = imgTopLeft
newEnemy.imgTopRight = imgTopRight
newEnemy.imgBottomLeft = imgBottomLeft
newEnemy.imgBottomRight = imgBottomRight
table.insert( enemies, newEnemy )
end
function UpdateEnemies()
for id, en in pairs( enemies ) do
if IsOnThisMap( en ) then
-- Enemy behavior
end
end
end
function DrawEnemies()
for id, en in pairs( enemies ) do
if IsOnThisMap( en ) then
spr( en.imgTopLeft, en.x-4, en.y-4, 0 )
spr( en.imgTopRight, en.x+4, en.y-4, 0 )
spr( en.imgBottomLeft, en.x-4, en.y+4, 0 )
spr( en.imgBottomRight, en.x+4, en.y+4, 0 )
print( en.health, en.x, en.y - 8 )
end
end
end
-- SETUP -----------------------------------------
-- MAPX MAPY TILEX TILEY HEALTH SPR1 SPR2 SPR3 SPR4
CreateEnemy( 0, 0, 1, 1, 3, 24, 25, 40, 41 )
-- TILE1X, TILE1Y, TILE2X, TILE2Y
CreateWarp( 57, 9, 62, 10 )
-- GAME LOOP -------------------------------------
function TIC()
map(mapx,mapy)
-- Draw player
spr(1,x2,y2,0)
spr(16,x2-8,y2+8,0)
spr(17,x2,y2+8,0)
spr(18,x2+8,y2+8,0)
spr(33,x2,y2+16,0)
spr(34,x2+8,y2+16,0)
spr(54,x2+8,y2,0)
spr(55,x2+16,y2,0)
spr(70,x2+8,y2+8,0)
spr(71,x2+16,y2+8,0)
spr(99,bx,by,0) -- bullet
spr(24,ex-4,ey-4,0) -- enemy top left
spr(25,ex+4,ey-4,0) -- enemy top right
spr(40,ex-4,ey+4,0) -- enemy bottom left
spr(41,ex+4,ey+4,0) -- enemy bottom right
UpdateEnemies()
DrawEnemies()
HandleWarps()
--print( "MAP: " .. mapx .. "," .. mapy, 5, 5, 12 )
--Debug( "enemies", enemies, 0, 0, 0 )
-- SHOW HEALTH
print(damage,x2,y2-8,12)
-- ADD TO DAMAGE
if GetDistance(x2,y2,ex,ey) <= 8 then
damage=damage+1
end
-- KILL PLAYER
if damage >= 150 then
x2=-100
end
-- KEYBOARD MOVEMENT
if btn (0) then y2=y2-1 -- up
elseif btn (1) then y2=y2+1 --down
elseif btn (2) then x2=x2-1 --left
elseif btn (3) then x2=x2+1 --right
end
-- SHOOTING WEAPON
if btn(4)then
bx=x2+16
by=y2
end
-- MOVE BULLET
bx=bx+5
end
--
-- 001:e0e0e0e00eeeeeee0333333003c33c3003f33f30033333300003300006666666
-- 002:0000000000000000000000000000000000000000000000000000000060000000
-- 016:0000000000000066000000660000006600000066000000000000000000000000
-- 017:0666666666666666666666666666666666666666066666660666666606666666
-- 018:6000000066666600666666006666660066666600666000006660000066600000
-- 020:2221222222212222222122221111111122222221222222212222222111111111
-- 024:00000554000004c5000004e40000044400000044000000550000555500005555
-- 025:554500004c5400004e4400004444000044400000555000005555500055555554
-- 033:0555555505555555055555550555500003355000033330000333300033333000
-- 034:5550000055500000555000003350000033500000333300003333333333333333
-- 040:4555555545555555455555550005555505555555555555550555505555555055
-- 041:5555555455555554555550005500000055000000555000005550000055500000
-- 048:0000000000000003000000030000000300000003000000000000000000000000
-- 049:3333300033333000333330003333330033333300000000000000000000000000
-- 050:0333333303333330033333300333333003333333000000000000000000000000
-- 051:0000000000000000000000000000000033000000000000000000000000000000
-- 054:eee00eeeeeeeeeeeeeeeeccceeeeeccceeeeeccceeeeeccceeeeeecceeeeeeee
-- 055:e0000000eeeeeecccceeeeccccceecccccccccccccccccccceeee000eeeee000
-- 070:0000eee00000ee000000ee0e0000eeee0000eeee0000ee000eeeee000eeeee00
-- 071:0eee0000eeee0000eee00000ee000000e0000000000000000000000000000000
-- 099:0000000000000000000009900333399903333999000009900000000000000000
--
--
--
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
--
--
-- 000:02000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020000b000000000
--
--
-- 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
--
--
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
--